package section

import (
	"hash/crc32"
)

func TableIndex(key any, num uint32) uint32 {
	var id uint32
	switch k := key.(type) {
	case string:
		id = HashKey(k)
	case int:
		id = uint32(k)
	}
	return id % num
}

func HashKey(key string) uint32 {
	if len(key) < 64 {
		//声明一个数组长度为64
		var srcatch [64]byte
		//拷贝数据到数组中
		copy(srcatch[:], key)
		//使用IEEE 多项式返回数据的CRC-32校验和
		return crc32.ChecksumIEEE(srcatch[:len(key)])
	}
	return crc32.ChecksumIEEE([]byte(key))
}
